package cn.ym.ymcc.web.controller;

import cn.ym.ymcc.dto.CourseDto;
import cn.ym.ymcc.service.ICourseService;
import cn.ym.ymcc.domain.Course;
import cn.ym.ymcc.query.CourseQuery;
import cn.ym.ymcc.result.JSONResult;
import cn.ym.ymcc.result.PageList;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;






    /**
     * 根据课程id查询课程详情
     * @param courseId 课程id
     * @return
     */
    @GetMapping(value = "/detail/data/{courseId}")
    public JSONResult detailByCourseId(@PathVariable("courseId")Long courseId){

        return JSONResult.success(courseService.detailByCourseId(courseId));
    }


    /**
     * 课程上线
     */
//    @PostMapping(value = "/onLineCourse/{id}")
//    public JSONResult onLineCourse(@PathVariable("id")Long id){
//        courseService.onLineCourse(id);
//        return JSONResult.success();
//    }

    @PostMapping(value = "/onLineCourse")
    public JSONResult onLineCourse(@RequestBody Long[] ids, HttpServletRequest request)  {
        courseService.onLineCourse(ids,request);
        return JSONResult.success();
    }
    /**
     * 课程下线
     */

    @PostMapping(value = "/offLineCourse")
    public JSONResult offLineCourse(@RequestBody Long[] ids, HttpServletRequest request){
        courseService.offLineCourse(ids,request);
        return JSONResult.success();
    }


    /**
     * 多个courseId 查询课程详情
     */
    @GetMapping(value="/info/{courseId}")
    public JSONResult info(@PathVariable("courseId") String courseId){

        return JSONResult.success( courseService.info(courseId));
    }


    /**
    * 保存和修改公用的
    */
    @RequestMapping(value="/save",method= RequestMethod.POST)
    public JSONResult saveOrUpdate(@RequestBody  @Valid CourseDto dto){
        if(dto.getId()!=null){
            courseService.updateCourseDto(dto);
        }else{
            courseService.saveCourseDto(dto);
        }
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @RequestMapping(value="/{id}",method=RequestMethod.DELETE)
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public JSONResult getCourseByCourseId(@PathVariable("id")Long id){

        return JSONResult.success(JSONObject.toJSONString(courseService.selectById(id)));
    }


    /**
    * 查询所有对象
    */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
    * 带条件分页查询数据
    */
    @RequestMapping(value = "/pagelist",method = RequestMethod.POST)
    public JSONResult page(@RequestBody CourseQuery query){
        return JSONResult.success(courseService.queryPage(query));
    }
}
